package scottb.flex;

import java.awt.*;

public class HGroupLayoutManager extends FlexLayoutManager {

    public Dimension preferredLayoutSize(Container container) {

        HGroup hgroup = (HGroup) container;

        Dimension[] childrenPreferredSizes = null;

        int gap = 0;

        if(null != hgroup.gap()) {
            gap = hgroup.gap();
        }

        int width = 0;

        if(hgroup.width() != null) {
            width = hgroup.width();
        }
        else {

            childrenPreferredSizes = getChildrenSizes(hgroup);

            boolean multiple = false;

            for(Dimension size : childrenPreferredSizes) {

                width += size.width;

                if(multiple) {
                    width += gap;
                }
                else {
                    multiple = true;
                }
            }
        }

        if(null != hgroup.paddingLeft()) {
            width += hgroup.paddingLeft();
        }

        if(null != hgroup.paddingRight()) {
            width += hgroup.paddingRight();
        }

        int height = 0;

        if(hgroup.height() != null) {
            height = hgroup.height();
        }
        else {

            if(null == childrenPreferredSizes) {
                childrenPreferredSizes = getChildrenSizes(hgroup);
            }

            for(Dimension size : childrenPreferredSizes) {

                if(size.height > height) {
                    height = size.height;
                }
            }
        }

        if(null != hgroup.paddingTop()) {
            height += hgroup.paddingTop();
        }

        if(null != hgroup.paddingBottom()) {
            height += hgroup.paddingBottom();
        }

        return new Dimension(width, height);
    }

    public void layoutContainer(Container container) {

        HGroup hgroup = (HGroup) container;

        int gap = 0;

        if(null != hgroup.gap()) {

            gap = hgroup.gap();
        }

        int marker = 0;

        if(null != hgroup.paddingLeft()) {

            marker = hgroup.paddingLeft();
        }

        for(Component childComponent : hgroup.getComponents()) {

            Dimension size = childComponent.getPreferredSize();
            int height = size.height;
            int width = size.width;

            int x = marker;
            int y = getY(hgroup, height);

            childComponent.setLocation(x, y);
            childComponent.setSize(width, height);
            childComponent.setBounds(x, y, width, height);

            marker += width;
            marker += gap;
        }
    }

    private int getY(HGroup hgroup, int height) {

        int y = 0;
        int paddingTop = 0;
        int paddingBottom = 0;

        if(null != hgroup.paddingTop()) {
            paddingTop = hgroup.paddingTop();
        }

        if(null != hgroup.paddingBottom()) {
            paddingBottom = hgroup.paddingBottom();
        }

        if(hgroup.align() != null) {

            if(hgroup.align() == VAlign.TOP) {

                y = paddingTop;
            }
            else if(hgroup.align() == VAlign.CENTER) {

                int hgroupHeight = hgroup.getHeight() - paddingTop - paddingBottom;

                y = hgroupHeight / 2 - height / 2;

                y += paddingTop;
            }
            else if(hgroup.align() == VAlign.BOTTOM) {

                y = hgroup.getHeight() - height - paddingBottom;
            }
        }
        else {

            y = paddingTop;
        }

        return y;
    }
}
